<template>
  <!-- 回收订单  上传打款截图  支付截图-->
  <div>
    <el-dialog :title="title" :visible="dialogVisible" :close-on-click-modal="false" @close="close" width="800px">
      <el-descriptions title="订单信息" :column="3" border :labelStyle="{minWidth:'80px',width:'80px',textAlign:'right'}" :contentStyle="{minWidth:'100px',textAlign:'left'}">
        <el-descriptions-item label="回收编号">{{orderInfo.no}}</el-descriptions-item>
        <el-descriptions-item label="游戏名称">{{orderInfo.gameName}}</el-descriptions-item>
        <el-descriptions-item label="游戏账号" :span="2">{{orderInfo.accountNumber}}</el-descriptions-item>
        <el-descriptions-item label="买家" v-if="ruleForm.dakuanType==3">{{orderInfo.buyerTenantName}}</el-descriptions-item>
        <el-descriptions-item label="回收价">
          <span style="color:red">¥{{orderInfo.backAmount}}</span>
        </el-descriptions-item>
        <el-descriptions-item label="包赔费" v-if="orderInfo.guaranteeTenantId">
          <span style="color:red">¥{{orderInfo.guaranteeAmount}}</span>
        </el-descriptions-item>
        <el-descriptions-item label="总金额">
          <span style="color:red">¥{{orderInfo.buyerActualAmount}}</span>
        </el-descriptions-item>
      </el-descriptions>
      <div style="height:20px"></div>
      <el-descriptions title="收款信息" :column="3" border :labelStyle="{minWidth:'80px',width:'80px',textAlign:'right'}" :contentStyle="{minWidth:'100px',textAlign:'left'}" v-if="orderInfo.guaranteeTenantId&&ruleForm.dakuanType==1">
        <el-descriptions-item label="包赔商" :span="3">{{orderInfo.guaranteeTenantName}}</el-descriptions-item>
        <el-descriptions-item label="账户类型">{{baopeiZhanghu.collectionAccountName}}</el-descriptions-item>
        <el-descriptions-item label="账户名称">{{baopeiZhanghu.collectionUserName}}</el-descriptions-item>
        <el-descriptions-item label="账户">{{baopeiZhanghu.collectionAccount}}</el-descriptions-item>
        <el-descriptions-item label="应付金额">
          <span style="color:red;font-size:16px;font-weight:bold">¥{{orderInfo.buyerActualAmount}}</span>
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="收款信息" :column="1" border :labelStyle="{minWidth:'80px',width:'80px',textAlign:'right'}" :contentStyle="{minWidth:'100px',textAlign:'left'}" v-if="!orderInfo.guaranteeTenantId||ruleForm.dakuanType==2">
        <el-descriptions-item label="卖家">{{orderInfo.sellerName}} <span v-if="orderInfo.orderType==2">({{ orderInfo.sellerTenantName }})</span></el-descriptions-item>
        <el-descriptions-item label="收款账户">{{sellerZhanghu}}</el-descriptions-item>
        <el-descriptions-item label="应付金额" v-if="ruleForm.dakuanType==1">
          <span style="color:red;font-size:16px;font-weight:bold">¥{{orderInfo.buyerActualAmount}}</span>
        </el-descriptions-item>
        <el-descriptions-item label="应付金额" v-if="ruleForm.dakuanType==2">
          <span style="color:red;font-size:16px;font-weight:bold">¥{{orderInfo.backAmount}}</span>
        </el-descriptions-item>
      </el-descriptions>
      <div style="height:20px"></div>
      <el-form :model="ruleForm" ref="ruleForm" :inline="true" label-width="80px" :rules="rules" class="demo-ruleForm new_form_box">
        <!-- <el-form-item label="备注:" prop="sqRemark">
          <el-input style="width:500px" type="textarea" v-model="ruleForm.sqRemark" rows="3" placeholder="请输入备注"></el-input>
        </el-form-item> -->
        <!-- bankList -->

        <el-form-item label-width="120px" label="买家收款账户:" prop="zhanghu" v-if="ruleForm.dakuanType==3">
          <el-select v-model="ruleForm.zhanghu" clearable @change="zhanghuChange">
            <el-option :label="item.collectionAccountName+'('+item.collectionUserName+item.collectionAccount+')'" :value="item.id" v-for="item in bankList" :key="item.id"></el-option>
          </el-select>
        </el-form-item><br />
        <el-form-item label="退款金额:" prop="amount" v-if="ruleForm.dakuanType==3">
          <el-input style="width:217px" type="text" v-model="ruleForm.amount" placeholder="请输入退款金额"></el-input>
        </el-form-item>
        <!-- 上传打款截图 -->
        <el-form-item v-if="ruleForm.dakuanType == 1" label="支付截图" prop="buyerImg" class="row">
          <uploadImageOne v-model="ruleForm.buyerImg"></uploadImageOne>
        </el-form-item>
        <el-form-item v-if="ruleForm.dakuanType == 2" label="放款截图" prop="guaranteeImg" class="row">
          <uploadImageOne v-model="ruleForm.guaranteeImg"></uploadImageOne>
        </el-form-item>

        <!-- 买家直接打款给卖家或包赔放款时 -->
        <!-- <el-form-item label="自动完成订单:" prop="" v-if="(ruleForm.dakuanType==1&&!orderInfo.guaranteeTenantId)||ruleForm.dakuanType==2">
          <div>
            <el-radio v-model="ruleForm.afterHandle" label="1">不完成</el-radio>
            <el-radio v-model="ruleForm.afterHandle" label="2">自动完成</el-radio>
          </div>
          <div class="tip">提示：如果选择自动完成，在财务打款完成后系统将自动完成订单交易。</div>
        </el-form-item> -->
        <!-- 包赔退款时 -->
        <!-- <el-form-item label="自动取消订单:" prop="" v-if="ruleForm.dakuanType==3">
          <div>
            <el-radio v-model="ruleForm.afterHandle" label="1">不取消</el-radio>
            <el-radio v-model="ruleForm.afterHandle" label="3">自动取消</el-radio>
          </div>
          <div class="tip">提示：如果选择自动取消，在财务打款完成后系统将自动取消订单交易。</div>
        </el-form-item> -->
        <!--  -->
        <!-- <el-form-item label-width="50px" label="备注:" prop="sqRemark" class="row">
          <el-input  type="textarea" v-model="ruleForm.sqRemark" rows="3" placeholder="请输入备注"></el-input>
        </el-form-item> -->
        <!--  -->
      </el-form>
      <div class="center">
        <el-button @click="close('ruleForm')">关闭</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="loading">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import payApi from "@/api/financeCenter/pay.js";
import onSelfApi from "@/api/retrieveManage/onSelf.js";
export default {
  name: "editPrice",
  data() {
    var priceRule = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        if (!this.priceReg.test(value)) {
          callback(new Error("请输入正确的退款金额"));
        } else if (this.orderInfo.buyerActualAmount < value) {
          callback(new Error("退款金额不能高于买家支付金额！"));
        }
        callback();
      }
    };
    return {
      title: "上传支付截图",
      dialogVisible: false,
      loading: false,
      uploadImgUrl: "",
      newImages: [],
      imgFileList: [],
      ruleForm: {
        id: "",
        sqRemark: "",
        img: "",
        zhanghu: "",
        amount: "",
        afterHandle: "1",
        guaranteeImg:'',
        buyerImg:''
      },
      orderInfo: {},
      baopeiZhanghu: {},
      sellerZhanghu: "",
      bankList: [],
      rules: {
        zhanghu: [
          {
            required: true,
            message: "请选择买家收款账户",
            trigger: "change",
          },
        ],
        amount: [
          {
            required: true,
            message: "请输入退款金额",
            trigger: "blur",
          },
          { validator: priceRule, trigger: "blur" },
        ],
        guaranteeImg: [
          {
            required: true,
            message: "请上传支付截图！",
            trigger: "change",
          },
        ],
        buyerImg: [
          {
            required: true,
            message: "请上传支付截图！",
            trigger: "change",
          },
        ],
      },
      userType: "",
    };
  },
  created() {
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
  },
  methods: {
    open(order, flag) {
      // console.log("%c Line:111 🍌 order", "color:#ffdd4d", order);
      this.loading = false;
      this.orderInfo = order;
      this.newImages = [];
      this.imgFileList = [];
      this.ruleForm.sqRemark = "";
      this.ruleForm.buyerImg = "";
      this.ruleForm.guaranteeImg = "";
      this.ruleForm.id = this.orderInfo.id;
      this.ruleForm.orderType = 3;
      this.ruleForm.dakuanType = flag;
      this.ruleForm.afterHandle = "1";
      if (flag == 2) {
        this.title = "支付截图";
      }
      if (flag == 3) {
        this.title = "申请退款";
        this.ruleForm.zhanghu = "";
        this.ruleForm.amount = Number(this.orderInfo.buyerActualAmount || 0);
        this.getBankList(this.orderInfo.buyerTenantId);
      }

      if (this.orderInfo.guaranteeTenantId) {
        if (this.orderInfo.guaranteeTenantCollection) {
          this.baopeiZhanghu = JSON.parse(
            this.orderInfo.guaranteeTenantCollection
          );
        }

        if (this.ruleForm.dakuanType == 2 && this.orderInfo.guaranteeInfo) {
          this.sellerZhanghu = JSON.parse(
            this.orderInfo.guaranteeInfo
          ).collectionRemark;
        }
      } else if (this.orderInfo.guaranteeInfo) {
        this.sellerZhanghu = JSON.parse(
          this.orderInfo.guaranteeInfo
        ).collectionRemark;
      }
      this.dialogVisible = true;
    },
    // 查询收款账户列表
    getBankList(tenantId) {
      // console.log("%c Line:181 🍔 tenantId", "color:#b03734", tenantId);
      this.loading = true;
      onSelfApi
        .getBankListByTenantId({
          tenantId: tenantId,
          accountType:1
        })
        .then((response) => {
          this.bankList = response.data;
          this.loading = false;
        });
    },
    // 选择包赔账户
    zhanghuChange(e) {
      if (e && this.bankList.length) {
        this.bankList.forEach((r) => {
          if (r.id == e) {
            // item.collectionAccountName+'('+item.collectionUserName+item.collectionAccount+')'
            this.ruleForm.details = `${r.collectionAccountName} ${r.collectionUserName} ${r.collectionAccount}`;
            //  this.ruleForm.guaranteeTenantCollection = JSON.stringify(r)
          }
        });
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // // 处理图片
          // let imgArr = this.imgFileList;
          // // 拼接新上传的图片
          // if (this.newImages && this.newImages.length) {
          //   let newArr = [];
          //   this.newImages.forEach((img) => {
          //     newArr.push({
          //       name: img.name,
          //       url: img.response.data.filePath,
          //     });
          //   });
          //   imgArr = imgArr.concat(newArr);
          // }
          // if (imgArr.length === 0) {
          //   return this.$message.error("请上传支付截图！");
          // }


          this.loading = true;
          onSelfApi.backClueOrderShangchuantu(this.ruleForm).then((res) => {
            this.dialogVisible = false;
            this.$message.success(res.msg);
            this.$parent.getList();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    // 图片上传成功
    handleUploadSuccess(res, file, fileList) {
      if (res.code == 200) {
        // 取得新上传的文件
        this.newImages = fileList.filter((t) => {
          return t.url.indexOf("blob:") > -1;
        });
      }
    },
    // 图片删除
    handleRemove(response, fileList, file) {
      const that = this;
      // 1.获取将要删除图片的临时路径
      const filePath = response.url;
      // 2.找到图片对应的索引值
      this.imgFileList.forEach(function (element, key) {
        if (element.url === filePath) {
          that.imgFileList.splice(key, 1); //回显的图片
        }
      });
      this.newImages.forEach(function (element, key) {
        if (element.url === filePath) {
          that.newImages.splice(key, 1); //新上传的图片
        }
      });
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url],
      });
    },
    uploadNumber(file, fileList) {
      //限制图片数量
      this.$modal.msgError("最多只能上传5张图片");
    },

    close(formName) {
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.tip {
  color: red;
}
::v-deep .el-dialog__body {
  padding-top: 10px !important;
}
::v-deep .el-upload-dragger {
  width: 148px;
  height: 148px;
}

.pick-dialog >>> .el-input--medium input {
  text-align: left;
}
</style>
